<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理案例</title>
    <link rel="stylesheet" href="./js/bootstrap.css">
    <script src="./js/jquery.js"></script>
    
</head>
<body style="margin: 15px;">
    <!-- 添加图书的panel 面板 -->
    
    <div class="panel panel-primary">
          <div class="panel-heading">
                <h3 class="panel-title">添加新图书</h3>
          </div>
          <!-- form-inline 内容在一行上显示 -->
          <div class="panel-body form-inline">
                
                <div class="input-group">
                    <div class="input-group-addon">书名</div>
                    <input type="text" class="form-control" id="iptBookname" placeholder="请输入书名">
                </div>

                <div class="input-group">
                    <div class="input-group-addon">作者</div>
                    <input type="text" class="form-control" id="iptAuthor" placeholder="请输入作者">
                </div>

                <div class="input-group">
                    <div class="input-group-addon">出版社</div>
                    <input type="text" class="form-control" id="iptPublisher" placeholder="请输入出版社">
                </div>

                <button id="btnAdd" class="btn btn-primary">添加</button>
                
          </div>
    </div>


    <!-- 表格区域 table-bordered 戴有边框的 表格-->
    
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tb">
            <!-- <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td><a href="javascript:;">删除</a></td>
            </tr> -->
        </tbody>
    </table>
    
    <script>
        $(function() {
            // 获取图书列表的数据
            function getBookList() {
                $.get('http://www.liulongbin.top:3006/api/getbooks',function(res) {
                    console.log(res);
                    if (res.status !== 200) return alert('获取数据失败')
                    var rows = ""
                    $.each(res.data, function(i, item) {
                       rows += ` <tr>
                                   <td>${item.id}</td>
                                   <td>${item.bookname}</td>
                                   <td>${item.author}</td>
                                   <td>${item.publisher}</td>
                                   <td><a href="javascript:;" class="del" data-id="${item.id}">删除</a></td>
                               </tr> `
                    });
                    // 把生成的行元素追加给  tbody
                    $("#tb").empty().html(rows);

                })
            }

            getBookList()
            
            // 通过事件委托，为动态添加的a标签绑定删除事件
            $("tbody").on("click", ".del", function() {
                // 获取自定义属性 attr
                var id = $(this).attr('data-id') 
                // console.log(id)
                $.get('http://www.liulongbin.top:3006/api/delbook', {id: id}, function(res) {
                    if (res.status !== 200 ) return alert('删除图书失败')
                    getBookList()
                })
            } )

            // 添加图书
            $("#btnAdd").on("click", function() {
                // 获取input输入框里面的内容
                var bookname = $('#iptBookname').val().trim()
                var author = $('#iptAuthor').val().trim()
                var publisher = $('#iptPublisher').val().trim()
                if (bookname.length <= 0 || author.length <= 0 || publisher.length <= 0) {
                  return alert('请填写完整的图书信息！')
                }

                $.post('http://www.liulongbin.top:3006/api/addbook', {bookname : bookname, author: author, publisher: publisher}, function(res) {
                    console.log(res)
                    if (res.status !== 201) return alert('添加图书失败')
                    getBookList()
                    $('#iptBookname').val('')
                    $('#iptAuthor').val('')
                    $('#iptPublisher').val('')
                })
                
            })

        })
    </script>
</body>
</html>